#include "PauseScreen.h"
#include "Graphics/GraphicsManager.h"
#include "StarComponents.h"

PauseScreen::PauseScreen(bool bIsLoadingScreen)
	: Object(),
	m_pTop(nullptr),
	m_pBottom(nullptr),
	m_PosY(0),
	m_FallSpeed(300.0f),
	m_bReversed(false),
	m_bIsLoadingScreen(bIsLoadingScreen),
	m_bIsFinished(false),
	m_WindowHeight(0)
{

}

PauseScreen::~PauseScreen()
{
}

void PauseScreen::Initialize()
{
	//AddComponent(new star::SpriteComponent(_T("HUD/ScreenOverlay_DarkGray.png"),_T("Overlay"), true));
	
	m_WindowHeight = star::GraphicsManager::GetInstance()->GetTargetWindowHeight();
	//Top Plane
	m_pTop = new star::Object();

	if(m_bIsLoadingScreen)
	{
		m_pTop->AddComponent(new star::SpriteComponent(_T("HUD/Loading_Top.png"), _T("LoadTop"), true, true));
	}
	else
	{
		m_pTop->AddComponent(new star::SpriteComponent(_T("HUD/Pause_Top.png"), _T("PauseTop"), true, true));
	}
	AddChild(m_pTop);
	m_PosY = static_cast<float>(m_WindowHeight);
	m_pTop->GetTransform()->Translate(0, m_PosY);

	//Bottom Plane
	m_pBottom = new star::Object();

	if(m_bIsLoadingScreen)
	{
		m_pBottom->AddComponent(new star::SpriteComponent(_T("HUD/Loading_Bottom.png"), _T("LoadBottom"), true, true));
	}
	else
	{
		m_pBottom->AddComponent(new star::SpriteComponent(_T("HUD/Pause_Bottom.png"), _T("PauseBottom"), true, true));
	}
	AddChild(m_pBottom);
	m_pBottom->GetTransform()->Translate(0, -m_PosY);
}

void PauseScreen::Update(const star::Context& context)
{
	if(!m_bReversed)
	{
		m_PosY -= m_FallSpeed * static_cast<float>(context.mTimeManager->GetSeconds());
		if(m_PosY < 0)
		{
			m_PosY = 0;
			m_bIsFinished = true;
		}
	}
	else
	{
		m_PosY += m_FallSpeed * 1.5f * static_cast<float>(context.mTimeManager->GetSeconds());
		if(m_PosY > static_cast<float>(m_WindowHeight))
		{
			m_PosY = static_cast<float>(m_WindowHeight);
			m_bReversed = false;
			SetDisabled(true);		
		}
	}
	
	m_pTop->GetTransform()->Translate(0, m_PosY);
	m_pBottom->GetTransform()->Translate(0, -m_PosY);
}

void PauseScreen::Reverse()
{
	m_bReversed = true;	
	m_PosY = 0;
}

bool PauseScreen::IsFinished() const
{
	return m_bIsFinished;
}

void PauseScreen::Reset()
{
	m_bIsFinished = false;
	m_PosY = star::GraphicsManager::GetInstance()->GetTargetWindowHeight();
	m_pTop->GetTransform()->Translate(0, m_PosY);
	m_pBottom->GetTransform()->Translate(0, -m_PosY);

	SetDisabled(true);
}